package com.itheima.web.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {


    /*
          init: 只执行一次
     */
    public void init() throws ServletException {

    }


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");

            // 获取方法名称
            String operation = request.getParameter("operation");
            // 获取Class对象
            Class clazz = this.getClass();
            // 获取方法对象
            Method method = clazz.getMethod(operation, HttpServletRequest.class, HttpServletResponse.class);
            // 调用方法
            method.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
